package com.book.service.impl;

import com.book.api.ReaderApi;
import com.book.pojo.Reward;
import com.book.pojo.RewardRanking;
import com.book.mapper.RewardRankingDao;
import com.book.service.RewardRankingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxy
 * @since 2023-10-14 11:35:22
 */
@Service
public class RewardRankingServiceImpl extends ServiceImpl<RewardRankingDao, RewardRanking> implements RewardRankingService {

    @Autowired
    private RewardRankingDao rewardRankingDao;
    @Autowired
    private ReaderApi readerApi;
    @Override
    public List<RewardRanking> RewardRanking() {
        List<Reward> rewards = readerApi.RewardRanking();
        RewardRanking ranking = new RewardRanking();
        for (Reward reward : rewards) {
            rewardRankingDao.deleteByBookId(reward.getBookId());
            ranking.setBookId(reward.getBookId());
            ranking.setRewardSum(reward.getRewardSum());
            rewardRankingDao.insert(ranking);
        }
        return rewardRankingDao.selectList(null);
    }

    @Override
    public void deleteByBookId(Long bookId) {
        rewardRankingDao.deleteByBookId(bookId);
    }

    @Override
    public void addRewardRanking(RewardRanking ranking) {
        rewardRankingDao.insert(ranking);
    }
}
